//
//  BalanceViewController.m
//  dangdang
//
//  Created by air on 16/1/26.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "BalanceViewController.h"
#import "KMPayTool.h"

@interface BalanceViewController ()

@property (nonatomic, strong) UIButton * tmpBtn;

@end

@implementation BalanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self updateUI];
    
    self.title = @"结算";
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payCallBack:) name:@"PayCallBack" object:nil];
}

-(void)payCallBack:(NSNotification *)noti
{
    UINavigationController *nav = self.navigationController;
    [nav popViewControllerAnimated:NO];
    [nav popViewControllerAnimated:NO];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (![_rechargeViewController.rechargeTextField.text isEqual:nil] && ![_rechargeViewController.rechargeTextField.text isEqualToString:@""]) {
        _moneyLabel.text = [NSString stringWithFormat:@"+%@", _rechargeViewController.rechargeTextField.text];
        
        _payMoneyLabel.text = [NSString stringWithFormat:@"￥%@",_rechargeViewController.rechargeTextField.text];
    }
    if ([_bill isEqualToString:@"bill"]){
        _titleLabel.text = @"支付金额";
        
        _moneyLabel.text = [NSString stringWithFormat:@"+%@", userObject.fee];
        
        _payMoneyLabel.text = [NSString stringWithFormat:@"￥%@",userObject.fee];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateUI{
    if (screenHeight == 480) {
        _topViewHeight.constant -= 10;
    }
    
    _submitButton.layer.cornerRadius = 8;
    _bottomView.hidden = YES;
}

- (IBAction)buttonSelect:(UIButton *)sender {
    if (_tmpBtn == nil){
        sender.selected = YES;
        _tmpBtn = sender;
    }
    else if (_tmpBtn !=nil && _tmpBtn == sender){
        sender.selected = YES;
    }
    else if (_tmpBtn!= sender && _tmpBtn!=nil){
        _tmpBtn.selected = NO;
        sender.selected = YES;
        _tmpBtn = sender;
    }
    _payLabel.text = labelArray[sender.tag];
    _bottomView.hidden = NO;
}
- (IBAction)submit:(UIButton *)sender {
//    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
//    view.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
//    [self.view.window addSubview:view];
//    self.view.window.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.4];
    
    NSUInteger tag = _tmpBtn.tag;
    NSString * token = userObject.accessToken;
    NSString * userID = userObject.userID;
    
    if(userID.length == 0)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"请先登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        return;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:token forKey:@"token"];
    [params setObject:@(tag) forKey:@"type"];
    [params setObject:userID forKey:@"uid"];
    [params setObject:_moneyLabel.text forKey:@"amount"];
    
    [self postRequestWithMethodName:@"/order/create_deposit_order" andParams:params andSender:nil];
    [LoadingView showInView:self.view image:nil info:@"请稍候..." animated:YES modeled:YES autoHide:NO];
    
//    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"“全民速送” 想要打开 “微信”" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *action = [UIAlertAction actionWithTitle:@"打开" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//    }];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
////        [view removeFromSuperview];
//    }];
//    [alertVC addAction:action];
//    [alertVC addAction:cancelAction];
//    [self presentViewController:alertVC animated:YES completion:nil];
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([code isEqualToString:@"0"]) {
            NSString *orderNum = [result objectForKey:@"retmsg"];
            
            if(orderNum.length>0)
            {
                NSUInteger tag = _tmpBtn.tag;
                if(tag == 0)
                {
                    //微信
                    NSDictionary *dic = [result objectForKey:@"paysign"];
                    NSMutableDictionary *infoDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                    [infoDic setObject:orderNum forKey:@"orderNum"];
                    [infoDic setObject:_rechargeViewController.rechargeTextField.text forKey:@"amount"];
                    [[KMPayTool sharedInstance] sendWXPay:infoDic];
                }
                else if(tag == 1)
                {
                    //支付宝
                    [[KMPayTool sharedInstance] sendAliPay:@{@"orderNum":orderNum,@"amount":_moneyLabel.text}];
                }
                else if(tag == 2)
                {
                    //银联
                }
            }
        }else{
            [self alertViewShowWithAlertString:@"电话号码或密码错误"];
        }
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PayCallBack" object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
